package tj.eskhata.pos.examples;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.Model;

import tj.eskhata.pos.*;


public class Output extends WebPage 
{
	public Output(final PageParameters parameters) {

        // Add the simplest type of label
		add(new Label("message", "It's OK Menn!"){
			@Override
			protected void onComponentTag(ComponentTag tag) {
				super.onComponentTag(tag);
				tag.put("style", "color:red");
			}
		}
		);
		
		add(new BookmarkablePageLink("link1",HomePage.class));
        add(new BookmarkablePageLink("link2",Output.class));
        
        /* Java code */
        final WebMarkupContainer parent = new WebMarkupContainer("comments");
        parent.setOutputMarkupId(true);
        add(parent);
        final List<String> comments = new ArrayList<String>();
        parent.add(new ListView("list", comments) {
	        @Override
	        protected void populateItem(ListItem item){	        
	        	item.add(new Label("comment", item.getModel()));
	        }
        });
        
        Form form = new Form("form");
        final TextArea editor = new TextArea("editor", new Model(""));
        editor.setOutputMarkupId(true);
        form.add(editor);
        form.add(new AjaxSubmitLink("save") {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form form) {
              comments.add((String)editor.getModelObject());
              editor.setModel(new Model(""));
              target.addComponent(parent);
              target.focusComponent(editor);
            }
          });
        parent.add(form);
        
        final WebMarkupContainer group = new WebMarkupContainer("group");
        add(group);
        group.add(new Label("dexter", "Omelette du fromage"));
        group.add(new Label("deedee", "That's all you can say!"));
        group.setOutputMarkupPlaceholderTag(true);
        add(new AjaxFallbackLink("link") {
	        @Override
	        public void onClick(AjaxRequestTarget target) {
		        group.setVisible(!group.isVisible());
		        if(target != null) {
		        	target.addComponent(group);
		        }
	        }
        });
    }
}
